<?php
include APPPATH . "third_party/wordnik/WordsApi.php";
require(APPPATH . 'third_party/wordnik/Swagger.php');

class Word_service extends MY_Controller {
	var $client;

	function __construct() {
		parent::__construct();
		$this->client = new APIClient(WORDNIK_KEY, WORDNIK_URL);
		
		// only ajax request can access this page
		if(!$this->input->is_ajax_request()) {
			return $this->show404(); 
		}
	}

	function isDefinitionSingularPluralPast($word,$def) { 
		if(preg_match("/[- ']/", $word)
			or preg_match("/^Simple past tense/", $def)
			or preg_match("/^Of, relating to/", $def)
			or preg_match("/é/", $word.$def)
			or preg_match("/^Third-person singular/", $def)
			or preg_match("/^A past tense/", $def)
			or preg_match("/^P(resent|ast) (tense|participle) /", $def)
			or preg_match("/^See /", $def)
			or preg_match("/^(Comparative|Superlative|Variant|Plural|Archaic)( form)? of /i", $def)
			or preg_match("/^The state of being /", $def)
			or preg_match("/^Able to be /", $def)
			or preg_match("/^(An )?(Obsolete|Alternative) (form|spelling|capitalization) of /i", $def)
			or preg_match("/^Common misspelling of /", $def)
			or preg_match("/^Incorrect spelling of /", $def)
			or preg_match("/^Plural form of /", $def)
			or preg_match("/^A (fe)?male given name/", $def)
			or preg_match("/^Same as /", $def)) {
				return true;
		}	
		return false;
	}
	
	function getRandomWord() {
		echo $this->test(); exit;

		$wordsApi = new WordsApi($this->client);
		$wordApi = new WordApi($this->client);

		$word = '-';
		$def = '';
		
		while ($this->isDefinitionSingularPluralPast($word, $def) ) {
			$rword = $wordsApi->getRandomWord();
			$word = $rword->word;
			$defs = $wordApi->getDefinitions($word);
			$recheck = FALSE;
			while ($defs == NULL) {
				$rword = $wordsApi->getRandomWord();
				$word = $rword->word;
				$defs = $wordApi->getDefinitions($word);
				$recheck = TRUE;
			}
			$def='';
			foreach ($defs as $d) {
				$def .= $d->text . "<br/>";
			}
			if ($recheck == TRUE) {
				continue;
			}
		}
		$this->session->set_userdata('definition',$def);
        echo json_encode(array("word" => $word, "definition" => $def));
	}
	
	function getRandomLastWord() {
		$this->load->library('LastWords');
		$lastWord = LastWords::getLastWord();
		$lastWord = 'Opposition'; ///////////////////////////////////// for testing only should be remove
		$this->session->set_userdata('lastword',$lastWord);
		echo json_encode(array('word'=>$lastWord));
	}

	function getDefinition($word = '', $chain = null) {
		if (!preg_match("/^\w+$/", $word)) {
			error_log("not only letters in word [$word]");
			exit;
		}
//		var_dump(stristr($this->session->userdata('definition'), $word));
		if(!stristr($this->session->userdata('definition'), $word) && $chain == null) {
			echo json_encode('failed');
			return;
		} else if(!stristr($this->session->userdata('definition'), $word) && 
			!array_key_exists($word, $this->session->userdata('chainWord'))) {
			echo json_encode('failed');
			return;		
		}
		
		if(stristr($this->session->userdata('lastword'), $word)) {
			echo json_encode('won');
			return;
		}
		$this->addToChainWordIfNotExist($word);
		$wordApi = new WordApi($this->client);
		$defs = $wordApi->getDefinitions(strtolower($word));
		foreach($defs as $def){
			$this->session->set_userdata('definition',$def->text);
			break;
		}
        echo json_encode($defs);
	}

	private function addToChainWordIfNotExist($word) {
		$chainWord = $this->session->userdata('chainWord');
		if(!array_key_exists($word, $chainWord)) {
			$chainWord[] = $word;
		}
		$this->session->set_userdata('chainWord',$chainWord);
	}

	
	private function test() {
		
		$definition = '{"word":"Luddism","definition":"Opposition to the Industrial Revolution by textile workers fearing for their livelihoods. \n Opposition to technological change. \n"}';
		$this->session->set_userdata('definition',$definition);
		return $definition;
	}
}